# coding=utf-8
"""
作者：董新强 
创建时间：2020-8-11
描述：问题管理
"""
from db_models.cat_questions import CatQuestions
from db_models.orm_base import OrmBase
from db_models.questions_deleted import QuestionsDeleted
from framework.db_session import DbSession
from framework.msg import failure, ok
from framework.utilities import to_int, to_string
from services.admin.admin_base import AdminBase
from services.admin.raw_question_manage import RawQuestionManage


class QuestionManage(AdminBase):
    def __init__(self, usr):
        super().__init__(usr)

    @AdminBase.admin(failure(msg='获取问题失败'))
    def get_detail(self, qid):
        with DbSession.create() as db:
            quesiton = db.query(CatQuestions.id,
                                CatQuestions.options,
                                CatQuestions.title,
                                CatQuestions.answer
                                ).filter(CatQuestions.id == qid).first()
            if not quesiton:
                return failure(msg='获取问题失败')
            return ok(data={
                'id': quesiton[0],
                'options': quesiton[1],
                'title': quesiton[2],
                'answer': quesiton[3]
            })

    @AdminBase.admin(failure(msg='获取问题失败'))
    def get_question_title_list(self, aid):
        ls = []
        with DbSession.create() as db:
            query = db.query(CatQuestions.id, CatQuestions.title).filter(CatQuestions.aid == aid).order_by(CatQuestions.id)
            for id, title in query:
                ls.append({'id': id, 'title': title})

        return ok(data=ls)

    @AdminBase.admin(failure(msg='获取已删除问题失败'))
    def get_deleted_question_list(self, aid):
        ls = []
        with DbSession.create() as db:
            query = db.query(QuestionsDeleted).filter(QuestionsDeleted.aid == aid).order_by(QuestionsDeleted.id)
            for v in query:
                ls.append(OrmBase.get_dict(v))

        return ok(data=ls)

    @AdminBase.admin(failure(msg='保存失败'))
    def update(self, reqs):
        qid = to_int(reqs.get('id'))
        if not qid:
            return failure(msg='参数错误')

        with DbSession.create() as db:
            question = db.query(CatQuestions).filter(CatQuestions.id == qid).first()
            if not question:
                return failure(msg='试题不存在')
            self.ufield(question, 'title', reqs)
            options = reqs.get('options')
            answer = reqs.get('answer')
            question.c, question.answer, question.type = RawQuestionManage.get_c(options, answer)
            question.options = to_string(options)

        return ok()

    @AdminBase.admin(failure(msg='删除失败失败'))
    def delete(self, qid):
        with DbSession.create() as db:
            question = db.query(CatQuestions).filter(CatQuestions.id == qid).first()
            if not question:
                return failure(msg='试题不存在')

            deleted = QuestionsDeleted.create(question)
            db.add(deleted)
            db.delete(question)
        return ok()

    @AdminBase.admin(failure(msg='恢复失败失败'))
    def recovery(self, qid):
        with DbSession.create() as db:
            delq = db.query(QuestionsDeleted).filter(QuestionsDeleted.id == qid).first()
            if not delq:
                return failure(msg='试题不存在')

            db.add(CatQuestions.recovery(delq))
            db.delete(delq)
        return ok()
